﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using WPFExcelReport.Helpers;
using System.Data.OracleClient;
using System.IO;
using INTEX.DataBase.Tools;
using INTEX.DataBase;
using System.Linq;
namespace WPFExcelReport
{
    public partial class YCAFB02Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new INTEX.DataBase.SETINTEXDB();
        #endregion
        public YCAFB02Report()
            : base()
        {


        }
        public YCAFB02Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                //exAgent._app.Visible = true;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                strCompany = "YCA";
                strExcelFile = @"YCA\YCAFB02.xls";
                Standard = "FB02DOC";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\YCA\YCAFB02.xls");
                destfolder = SetFolder(Root, "成衣库存");
                INTEXDBC.OracleDB = OracleDB;
                INTEXDBC.proxy = proxy; 

                string newfilename =year + "_" + month + "_" + day;
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile(newfilename, fileType);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\成衣库存" + fileabsname + ".xls"; ;
                destfile = filename;
                if (File.Exists(fileabsname + ".xls"))
                {
                    File.Delete(fileabsname + ".xls");
                }
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        public void FillInExcelData(int xStart = 1, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = dsSource.Tables["ICC_FB02"];
                DataTable dtOutput = new DataTable();
       
                dtOutput.Columns.Add("LSNAME1", typeof(System.String));
                dtOutput.Columns.Add("FBYEAR", typeof(System.String));
                dtOutput.Columns.Add("FBMONTH", typeof(System.String));
                dtOutput.Columns.Add("FBDAY", typeof(System.String));
                dtOutput.Columns.Add("FSARTGRP", typeof(System.String));
                dtOutput.Columns.Add("FSMATART", typeof(System.String));
                dtOutput.Columns.Add("FBART", typeof(System.String));
                dtOutput.Columns.Add("FSABEZ", typeof(System.String));
                dtOutput.Columns.Add("FBAUF", typeof(System.String)); 
                dtOutput.Columns.Add("FBFARBE", typeof(System.String));
                dtOutput.Columns.Add("FBGROE", typeof(System.String));
                dtOutput.Columns.Add("FBARCODE", typeof(System.String)); 
                dtOutput.Columns.Add("FBSTDPRICE", typeof(System.String));
                dtOutput.Columns.Add("FBEKPRICE", typeof(System.String));
                dtOutput.Columns.Add("FBME", typeof(System.Decimal));
                dtOutput.Columns.Add("FBREKPRICE", typeof(System.String));
                dtOutput.Columns.Add("FBEKVALUE", typeof(System.String));
                dtOutput.Columns.Add("FBSTDVALUE", typeof(System.String));    
                dtOutput.Merge(dtSoruce,false,MissingSchemaAction.Ignore);
                foreach (DataRow row in dtOutput.Rows)
                {
                    row["LSNAME1"] = GetGB2312Value(row["LSNAME1"].ToString());
                    row["FSARTGRP"] = GetGB2312Value(row["FSARTGRP"].ToString());
                    row["FSMATART"] = GetGB2312Value(row["FSMATART"].ToString());
                    row["FSABEZ"] = GetGB2312Value(row["FSABEZ"].ToString());
                }
                exAgent.DataTableToExcel(dtOutput, xStart, yStart);
                int count = dtOutput.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(2, 5, count + 1, 6);
                exAgent.SetCellValue(1, 1, "");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private string CreateFile(string ordernumer)
        {
            string filename = "";
            string sheetName = "";
            string newfilename = "";
            try
            {
                string pwd = "";
                sheetName = ordernumer;
                newfilename = "成衣库存_" + ordernumer + ".xls";
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(newfilename, Standard, sheetName, pwd);
                filename = exAgent.FileName;
                FillInExcelData(5,1);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.Save();
                exAgent.Close();
            }
            return filename;
        }
    }
}